<?php

namespace App\Http\Controllers\Admins;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;

class LoginController extends Controller
{
    /**
     * 后台登录界面
     *
     * @return 
     */
    public function login()
    {
        //
        return view('webadmin.login');
    }

    public function dologin(Request $request)
    {
        //获取用户名
        $username = $request->username;

        //读取数据库
        $user = User::where('name',$username)->where('status',2)->first();

        //判断
        if(empty($user)){
            return back()->with('info','用户名不存在');
        }

        //检测密码
        if(Hash::check($request->password,$user->password)){
            //写入session
            session(['id' => $user->id]);  

            return redirect('/admin')->with('info','登录成功');
        }else{
            return back();
        }

    }

    public function logout(Request $request)
    {
        $request->session()->flush();
        return redirect('/admin/login');
    }

}   
